package ru.onlinestore.mobile.domain.usecase

import kotlinx.coroutines.flow.*
import ru.onlinestore.mobile.core.Response
import ru.onlinestore.mobile.domain.repository.ProductsRepository
import ru.onlinestore.mobile.ui.entities.*

class GetProductListUseCase(
    private val repository: ProductsRepository
) {

    operator fun invoke(): Flow<Response<ProductListUI>> = flow {
        emit(Response.Loading())
        emit(
            repository.fetchProductList().map { list ->
                ProductListUI(
                    products = list.products.map { product ->
                        ProductUI(
                            id = product.id,
                            title = product.title,
                            description = product.description,
                            price = product.price,
                            brand = product.brand,
                            category = product.category,
                            thumbnail = product.thumbnail,
                            images = product.images
                        )
                    },
                    total = list.total,
                    skip = list.skip,
                    limit = list.limit
                )
            }
        )
    }
}